<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>计划管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/frontend/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/frontend/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <div class="layui-btn-group">
                {{#  if(d.status == '未开始'){ }}
                <button type="button" class="generate layui-btn layui-btn-xs" lay-event="generate">生成</button>
                {{#  } else { }}
                <button type="button" class="generate layui-btn layui-btn-xs layui-btn-disabled" lay-event="generate" disabled="disabled">生成</button>
                {{# } }}
                <button type="button" class="subPlan layui-btn layui-btn-xs" lay-event="subPlan">子计划</button>
                {{#  if(d.status == '未开始'){ }}
                <button type="button" class="task layui-btn layui-btn-xs layui-btn-disabled" lay-event="task" disabled="disabled">任务</button>
                {{#  } else { }}
                <button type="button" class="task layui-btn layui-btn-xs" lay-event="task">任务</button>
                {{# } }}
            </div>
            <div class="layui-btn-group">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">详情</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            </div>
        </script>
        <script type="text/html" id="priority">
            {{#  if(d.priority == '低'){ }}
            <span class="layui-badge layui-bg-cyan">{{d.priority}}</span>
            {{#  } else if(d.priority == '中') { }}
            <span class="layui-badge layui-bg-orange">{{d.priority}}</span>
            {{#  } else { }}
            <span class="layui-badge">{{d.priority}}</span>
            {{# } }}
        </script>
    </div>
</div>
<script src="/static/frontend/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/frontend/js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'miniTab'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniTab = layui.miniTab;

        table.render({
            elem: '#currentTableId',
            height: 'full-60',
            url: '/frontend/plan/list?status=all&project=all&requirement={$requirementId}',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'project', width: 100, title: '项目'},
                {field: 'name', width: 160, title: '名称'},
                {field: 'start_time', width: 160, title: '开始时间'},
                {field: 'end_time', width: 160, title: '结束时间'},
                {field: 'status', width: 80, title: '状态'},
                {field: 'priority', width: 90, title: '优先级', templet: '#priority', sort: true},
                {title: '操作', minWidth: 200, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑计划',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: false,
                    area: ['800px', '420px'],
                    content: '/frontend/plan/edit?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'generate') {
                $.post('/frontend/plan/generate', {id:data.id}, function (res) {
                    if (res.code == 0) {
                        $(obj.tr).find('.generate').addClass('layui-btn-disabled').attr('disabled', 'disabled');
                        $(obj.tr).find('.task').removeClass('layui-btn-disabled').removeAttr('disabled');
                        //同步更新缓存对应的值
                        obj.update({
                            status: '进行中'
                        });
                        layer.msg(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
                return false;
            } else if (obj.event === 'task') {
                // 打开新的窗口
                miniTab.openNewTabByIframe({
                    href:"/frontend/plan/task?id=" + data.id,
                    title:"计划任务",
                });
                return false;
            }  else if (obj.event === 'subPlan') {
                var index = layer.open({
                    title: '子计划',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: false,
                    area: ['100%', '100%'],
                    content: '/frontend/plan/sub-plan/index?pid='+data.id,
                    cancel: function(index, layero){
                        // 重新加载父页面
                        location.reload();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除吗？', {icon:3, title:'操作确认'}, function (index) {
                    layer.close(index);
                    // 向服务端发送删除指令
                    $.post('/frontend/plan/delete', {id:data.id}, function (res) {
                        if (res.code == 0) {
                            obj.del();
                            layer.msg(res.msg, {icon: 1});
                        } else if (res.code == 302) {
                            location.href = '/frontend/error/' + res.msg;
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'json');
                });
                return false;
            }
        });

    });
</script>
</body>
</html>